package com.kch.web.controller.system;

import com.kch.common.annotation.Log;
import com.kch.common.core.controller.BaseController;
import com.kch.common.core.domain.AjaxResult;
import com.kch.common.core.page.TableDataInfo;
import com.kch.common.enums.BusinessType;
import com.kch.framework.util.ShiroUtils;
import com.kch.system.domain.JoinUs;
import com.kch.system.service.IJoinUsService;
import com.kch.system.vo.JoinVO;
import com.kch.web.config.CloumnProperties;
import com.kch.web.controller.common.CheckNumberUtils;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/system/join")
@Api(tags = "加入我们")
public class JoinController extends BaseController {

    private String prefix = "system/join";
    @Autowired
    private IJoinUsService joinUsService;
    @Autowired
    private CloumnProperties cloumnProperties;

    @RequiresPermissions("system:join:view")
    @GetMapping()
    public String index(ModelMap mmap) {
        return prefix + "/join";
    }

    @PostMapping("/list")
    @RequiresPermissions("system:join:list")
    @ResponseBody
    public TableDataInfo list() throws Exception {
        startPage();
        List<JoinUs> joins = joinUsService.selectList();
        return getDataTable(joins);
    }

    @GetMapping("/add")
    public String add(ModelMap mmap) {
        return prefix + "/add";
    }

    @Log(title = "领域联盟", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated JoinVO joinVO) throws Exception {
        /*if(!ObjectUtils.isEmpty(joinVO.getYear())){
            if(!CheckNumberUtils.check(String.valueOf(joinVO.getYear()))){
                return AjaxResult.error("工作年限必须为整型");
            }
        }*/
        JoinUs joinUs = new JoinUs();
        BeanUtils.copyProperties(joinVO, joinUs);
        joinUs.setYear(joinVO.getYear());
        joinUs.setCreateTime(new Date());
        joinUs.setCreateBy(ShiroUtils.getLoginName());
        joinUsService.insertJoinUs(joinUs);
        return toAjax(true);
    }

    @Log(title = "加入我们", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(joinUsService.deleteById(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) throws Exception {
        mmap.put("join", joinUsService.selectById(id));
        return prefix + "/edit";
    }

    @Log(title = "加入我们", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated JoinVO joinVO) {
        try {
            /*if(!ObjectUtils.isEmpty(joinVO.getYear())){
                if(!CheckNumberUtils.check(String.valueOf(joinVO.getYear()))){
                    return AjaxResult.error("工作年限必须为整型");
                }
            }*/
            JoinUs joinUs = joinUsService.selectById(joinVO.getId());
            joinVO.setCreateTime(joinUs.getCreateTime());
            joinVO.setCreateBy(joinUs.getCreateBy());
            BeanUtils.copyProperties(joinVO, joinUs);
            joinUs.setUpdateTime(new Date());
            joinUs.setCreateBy(ShiroUtils.getLoginName());
            joinUs.setYear(joinVO.getYear());
            joinUsService.updateById(joinUs);
            return toAjax(true);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }
}
